package cn.libinal.scloud.base.service;

import cn.libinal.scloud.base.entity.Menu;
import cn.libinal.scloud.base.entity.Permission;
import cn.libinal.scloud.base.entity.Role;
import cn.libinal.scloud.base.entity.User;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.micrometer.core.instrument.util.StringUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;

public class DataBuilder {

    public static User singleUser() {
        User user = new User();
        user.setUsername("admin");
        user.setPassword("123456");
        user.setPhone("18900001111");
        user.setEmail("admin@test.com");
        user.setBirthday(LocalDate.of(1999, 1, 1));
        user.setAddress("cn");
        user.setEnabled(true);
        return user;
    }

    public static List<User> buildUsers(int size) {
        List<User> users = Lists.newArrayList();
        for (int i = 0; i < size; i++) {
            User user = new User();
            user.setUsername("user" + i);
            user.setPassword("123456");
            user.setPhone("18900001111");
            user.setEmail("user" + i + "@test.com");
            user.setBirthday(LocalDate.of(1999, 1, 1 + i));
            user.setAddress("cn" + i);
            users.add(user);
        }
        return users;
    }

    public static Role singleRole() {
        Role role = new Role();
        role.setName("系统管理员");
        role.setSn("ADMIN");
        role.setPriority(1);
        return role;
    }

    public static Set<Role> buildRoles(int size) {
        Set<Role> roles = Sets.newHashSet();
        for (int i = 0; i < size; i++) {
            Role role = new Role();
            role.setName("测试角色" + i);
            role.setSn("TEST" + i);
            role.setPriority(i + 1);
            roles.add(role);
        }
        return roles;
    }

    public static Set<Menu> buildMenus(int size, String sn) {
        Set<Menu> menus = Sets.newHashSet();
        if (StringUtils.isBlank(sn)) {
            sn = "TEST_MENU";
        }
        for (int i=0;i<size;i++) {
            Menu menu = new Menu();
            menu.setName("菜单" + i);
            menu.setSn(sn + "_" + i);
            menu.setUrl("test/menus/index" + i);
            menu.setPriority(i);
            menus.add(menu);
        }
        return menus;
    }

    public static Set<Permission> buildPermissions(String sn) {
        Set<Permission> permissions = Sets.newHashSet();
        Permission permission = new Permission();
        permission.setName("查看");
        permission.setPriority(0);
        permission.setSn(sn+"_VIEW");
        permissions.add(permission);
        permission = new Permission();
        permission.setName("新增");
        permission.setPriority(1);
        permission.setSn(sn+"_CREATE");
        permissions.add(permission);
        permission = new Permission();
        permission.setName("更新");
        permission.setPriority(2);
        permission.setSn(sn+"_UPDATE");
        permissions.add(permission);
        permission = new Permission();
        permission.setName("删除");
        permission.setPriority(3);
        permission.setSn(sn+"_DELETE");
        permissions.add(permission);
        return permissions;
    }
}
